package com.tarena.lbs.user.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.service.UserGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserGroupServiceImpl implements UserGroupService {
    @Autowired
    private UserGroupRepository userGroupRepository;
    @Autowired
    private AdminRepository adminRepository;
    @Override
    public PageResult<UserGroupVO> pageList() {

        PageResult<UserGroupVO> pageResult = new PageResult<>();
        List<UserGroupPO> userGroupPOS = userGroupRepository.selectGrouplist();
        log.info("查询到的数据列表为：{}", userGroupPOS);

        PageInfo<UserGroupPO> pageInfo = new PageInfo<>(userGroupPOS);
        pageResult.setPageNo(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setTotal(pageInfo.getTotal());

        List<UserGroupVO> vos = new ArrayList<>();
        List<UserGroupPO> pos = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(pos)) {
            vos = pos.stream().map(po -> {
                UserGroupVO vo = new UserGroupVO();
                BeanUtils.copyProperties(po, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setObjects(vos);
        log.info("返回的分页结果为：{}", pageResult);
        return pageResult;


    }

    @Override
    public void save(UserGroupParam param) {
        userGroupRepository.save(param);
    }



}

